package culton.android.sms; 

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class SMSReceiver extends BroadcastReceiver
{	
	@Override
	public void onReceive(Context context, Intent intent) 
	{
        //Get the SMS message passed in
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        if (bundle != null)
        {
            //Retrieve the SMS message received
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            
            if (msgs.length > 1) {
            	//do something
            } else {
            	msgs[0] = SmsMessage.createFromPdu((byte[])pdus[0]);
            	String messageReceived = msgs[0].getMessageBody().toString();
            	
            	if (messageReceived.contains("CN:<M")) {
            		//Receive movies from server via SMS
            		setMoviesAndDisplay(messageReceived, context);
            		
            	} else if (messageReceived.contains("CN:<S")) {
            		//Receive movie showtimes from server via SMS
            		setShowtimes(messageReceived, context);
            	}
            }
            
            /*for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                String msg = msgs[i].getMessageBody().toString();                
            }*/            
        }                 		
	}
	
	private static void setMoviesAndDisplay(String messageReceived, Context context) {
		String message =  messageReceived.substring(5);
		SMSMoviesActivity.OnMessageReceived(message);
	}
	
	private static void setShowtimes(String messageReceived, Context context) {
		SMSDetailsActivity.OnMessageReceived(messageReceived);		
	}
}